<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
    <% String path=request.getContextPath(); String basePath=request.getScheme() + "://" + request.getServerName() + ":"
        + request.getServerPort() + path + "/" ; %>

        <!DOCTYPE html>
        <html>

        <head>
            <base href="<%=basePath%>">

            <title>用户管理</title>

            <meta http-equiv="pragma" content="no-cache">
            <meta http-equiv="cache-control" content="no-cache">
            <meta http-equiv="expires" content="0">
            <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
            <meta http-equiv="description" content="This is my page">
            <!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

        </head>

        <body>
            <%@ include file="/WEB-INF/jsp/common/header.jsp" %>
                <%@ include file="/WEB-INF/jsp/common/loadDiv.jsp" %>

                    <script type="text/javascript">
                        $(function () {
                            bindtree();
                            bindgrid();
                            binduserdept();
                            bindcombogrid();
                        });
                    </script>
                    <script>
                        function del(node) {
                            $.ajax({
                                type: "post",
                                url: "UserManage/DelUser",
                                //                dataType: "xml",
                                data: {
                                    userid: node.userid,
                                    czr: PUser.loginname
                                },
                                success: function (data) {
                                    $.messager.alert('消息', data);
                                    $('#add').window('close');
                                    bindgrid();
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    $.messager.alert('消息', errorThrown);
                                }
                            });
                        }

                        function getroles(node, dd) {
                            $.ajax({
                                type: "post",
                                url: "UserManage/GetRoles",
                                //                dataType: "xml",
                                data: {
                                    userid: node.userid
                                },
                                async: false,
                                success: function (data) {
                                    $('#roleselect').combogrid('setValues', data.split(','));
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    $.messager.alert('消息', errorThrown);
                                }
                            });
                        }

                        function save() {
                            if (!$('#ssec-form').form('validate')) {
                                return;
                            }
                            var a = $('#t1').val();
                            var c = $('#t3').val();
                            if (c == '') {
                                $.messager.alert('消息', '请输入登录名');
                                return;
                            }
                            var d = $('#t4').textbox('getText');
                            if (d == '') {
                                $.messager.alert('消息', '请输入密码');
                                return;
                            }
                            var b = $('#t2').val();
                            if (b == '') {
                                $.messager.alert('消息', '请输入姓名');
                                return;
                            }
                            var e = $('#t5').combobox('getValue');
                            var f = $('#t6').prop("checked");
                            var g = $('#t7').val();
                            var roles = $('#roleselect').combogrid('getValues');
                            if (roles == '') {
                                $.messager.alert('消息', '请选择角色');
                                return;
                            }
                            var rolesid = '';
                            for (var i = 0; i < roles.length; i++) {
                                rolesid = rolesid + roles[i] + ',';
                            }
                            rolesid = rolesid.substr(0, rolesid.length - 1);
                            var dept = $('#userdept').combogrid('getValue');
                            if (dept == '') {
                                $.messager.alert('消息', '请选择机构');
                                return;
                            }
                            var phone = $('#phone').val();
                            //if (phone == '') {$.messager.alert('消息','请输入电话'); return; }
                            var tel = $('#txtTel').val();
                            var mail = $('#txtMail').val();
                            var datapermission = "";
                            var cmm_code_ids = $('#datapermission').combotree('getValues');
                            if (cmm_code_ids.length > 0) {
                                for (var i = 0; i < cmm_code_ids.length; i++) {
                                    datapermission += cmm_code_ids[i] + ",";
                                }
                                if (datapermission.indexOf(",") > -1) {
                                    datapermission = datapermission.substring(0,
                                        datapermission.length - 1)
                                }
                            }

                            var btn_ids = $('#txtBtnPermission').combobox('getValues');
                            var btnpermission = btn_ids.join(',');
                            if (btn_ids.length > 0) {
                                // for (var i = 0; i < btn_ids.length; i++) {
                                // 	btnpermission += btn_ids[i];
                                // }
                                // if (btnpermission.indexOf(",") > -1) {
                                // 	btnpermission = btnpermission.substring(0,
                                // 		btnpermission.length - 1)
                                // }
                            }
                            $.ajax({
                                type: "post",
                                url: "UserManage/AddUser",
                                //                dataType: "xml",
                                data: {
                                    userid: a,
                                    loginname: c,
                                    pwd: d,
                                    username: b,
                                    disabled: e,
                                    superadmin: f ? '1' : '0',
                                    sortby: g,
                                    roles: rolesid,
                                    userdept: dept,
                                    phone: phone,
                                    tel: tel,
                                    mail: mail,
                                    czr: PUser.userid,
                                    casid: $('#txtCAS').val(),
                                    sap: $('#sap').val(),
                                    oa: $('#oa').val(),
                                    wxid: $('#txtWX').val(),
                                    hlevel: $('#hlevel').val(),
                                    datapermission: datapermission,
                                    oaDeptId: $('#oaDeptId').textbox('getText'),
                                    oaDeptName: $('#oaDeptName').textbox('getText'),
                                    btnPermission: btnpermission
                                },
                                success: function (data) {
                                    $.messager.alert('消息', data);
                                    if (data == '保存成功!') {
                                        $('#add').window('close');
                                        bindgrid();
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    $.messager.alert('消息', errorThrown);
                                }
                            });
                        }

                        function editrow() {
                            //编辑用户
                            var user = $('#dg').datagrid('getSelected');
                            if (user) {
                                $('#add').window('open');
                                $('#t1').val(user.userid);
                                $('#t2').val(unescape(user.username));
                                // 								$('#t4').val(user.pwd);
                                // 								alert(unescape(user.pwd));
                                $('#t3').val(unescape(user.loginname));
                                $('#t4').textbox('setText', unescape(user.pwd));
                                $('#t5').combobox('setValue', user.disabled);
                                //if (user.DISABLED == "1")
                                //    $('#t5').attr('checked', true);
                                //else
                                //    $('#t5').attr('checked', false);
                                if (user.superadmin == "1")
                                    $('#t6').attr('checked', true);
                                else
                                    $('#t6').attr('checked', false);
                                $('#t7').numberbox('setValue', user.usersort);
                                $('#phone').val(user.phone);
                                $('#txtTel').val(unescape(user.tel));
                                $('#txtMail').val(user.mail);
                                $('#txtCAS').val(user.casid);
                                $('#txtWX').val(user.wxid);
                                $('#sap').val(user.sap);
                                $('#hlevel').val(user.hlevel);
                                //$('#oa').setValue(user.oa);
                                $('#oa').numberbox('setValue', user.oa);
                                //combo tree grid
                                $('#roleselect').combogrid('clear');
                                $('#userdept').combotree('setValue', user.deptid);
                                $('#deptCode').textbox('setValue', user.deptCode);
                                $('#oaDeptId').textbox('setValue', user.oaDeptId);
                                $('#oaDeptName').textbox('setValue', user.oaDeptName);
                                $('#datapermission').combotree('setValues',
                                    user.datapermission.split(','));
                                if (user.btnPermission) {
                                    $('#txtBtnPermission').combobox('setValues', user.btnPermission.split(','));
                                } else {
                                    $('#txtBtnPermission').combobox('setValues', '')
                                }
                                //var sv = Xiaov.SmartWeb.Application.SysAdmin.Admin_User_CL.GetRolesIdByUserId(user.USERID).value;
                                ////$('#roleselect').combogrid('setValues', Xiaov.SmartWeb.Application.SysAdmin.Admin_User.GetRolesIdByUserId(user.USERID).value);
                                var roles = getroles(user);
                                //$('#roleselect').combogrid('setValues', roles.split(','));

                            } else {
                                $.messager.alert('消息', '请选中一个用户');
                            }
                        }
                    </script>
                    <script>
                        function bindcombogrid() {
                            $('#roleselect').combogrid(
                                {
                                    multiple: true,
                                    panelWidth: 400,
                                    idField: 'roleid',
                                    textField: 'rolename',
                                    singleSelect: true,
                                    url: 'RoleManage/RoleListJson_combogrid?userid='
                                        + PUser.userid,
                                    columns: [[{
                                        field: 'ck',
                                        checkbox: true
                                    }, {
                                        field: 'roleid',
                                        title: '角色编码',
                                        width: 10,
                                        hidden: true
                                    }, {
                                        field: 'rolename',
                                        title: '角色名称',
                                        width: 100,
                                        formatter: function (val, rec) {
                                            return unescape(val)
                                        }
                                    }
                                        //{ field: 'DISABLED', title: '禁用', width: 70, align: 'center', formatter: function (val, rec) { if (val == 0) return '否'; else return true; } },
                                        //{ field: 'ROLECOMMENT', title: '备注', width: 150, align: 'left', formatter: function (val, rec) { return unescape(val) } }
                                    ]],
                                    fitColumns: true
                                });
                        }

                        function binduserdept() {
                            $('#userdept').combotree({
                                url: 'DeptManage/DeptListJson_combobox?deptid=' + PUser.deptid
                            });
                            $('#datapermission').combotree({
                                multiple: true,
                                // 				value : [ 'rlzyb', '10', '100' ],
                                url: 'DeptManage/DeptListJson_combobox?deptid=' + PUser.deptid
                            });
                        }

                        function bindtree() {
                            $('#tt').treegrid({
                                title: '组织机构列表',
                                url: 'DeptManage/DeptListJson?deptid=' + PUser.deptid,
                                iconCls: 'icon-applicationgo',
                                nowrap: true,
                                width: 300,
                                height: 400,
                                animate: false,
                                collapsible: false,
                                singleSelect: true,
                                idField: 'deptid',
                                treeField: 'dwmc',
                                columns: [[{
                                    field: 'deptid',
                                    title: '部门id',
                                    width: 50,
                                    hidden: true
                                }, {
                                    field: 'dwmc',
                                    title: '名称',
                                    width: 240,
                                    formatter: function (val, rec) {
                                        return unescape(val)
                                    }
                                }]],
                                onClickRow: function () {
                                    bindgrid();
                                },
                                onLoadSuccess: function () {
                                    $('#tt').treegrid('collapseAll');
                                    var rows = $('#tt').treegrid('getChildren');
                                    if (rows.length) {
                                        $('#tt').treegrid('expand', rows[0].dwbm);
                                    }
                                }
                            });
                        }

                        function bindgrid() {
                            var dept;
                            var node = $('#tt').treegrid('getSelected');
                            if (node != null) {
                                dept = node.deptid;
                            }
                            var name = $('#searchName').val();
                            var url = 'UserManage/UserListJson?a=1';
                            if (name) {
                                url = url + '&name=' + name;
                            } else {
                                url = url + '&dwbm=' + dept;
                            }
                            // 			alert(name);
                            $('#dg').datagrid(
                                {
                                    title: '用户列表',
                                    url: url,
                                    iconCls: 'icon-application',
                                    width: 700,
                                    height: 400,
                                    nowrap: true,
                                    rownumbers: true,
                                    animate: false,
                                    collapsible: false,
                                    singleSelect: true,
                                    remoteSort: false,
                                    columns: [
                                        [
                                            {
                                                field: 'userid',
                                                title: 'id',
                                                sortable: true,
                                                // 										hidden: true,
                                                formatter: function (val, rec) {
                                                    return unescape(val)
                                                }
                                            }, {
                                                field: 'loginname',
                                                title: '登录名',
                                                sortable: true,
                                                formatter: function (val, rec) {
                                                    return unescape(val)
                                                }
                                            }, {
                                                field: 'username',
                                                title: '姓名',
                                                sortable: true,
                                                formatter: function (val, rec) {
                                                    return unescape(val)
                                                }
                                            }, {
                                                field: 'deptCode',
                                                title: '部门编码',
                                                sortable: true,
                                                formatter: function (val, rec) {
                                                    if (val != null) {
                                                        return unescape(val)
                                                    }
                                                }
                                            }, {
                                                field: 'deptName',
                                                title: '部门名称',
                                                sortable: true,
                                                formatter: function (val, rec) {
                                                    if (val != null) {
                                                        return unescape(val)
                                                    }
                                                }
                                            }, {
                                                field: 'sap',
                                                title: 'sap采购组',
                                                sortable: true,
                                                formatter: function (val, rec) {
                                                    return unescape(val)
                                                }
                                            }, {
                                                field: 'wxid',
                                                title: 'HI账号',
                                                sortable: true,
                                                formatter: function (val, rec) {
                                                    return unescape(val)
                                                }
                                            },
                                            {
                                                field: 'disabled',
                                                title: '启用禁用',
                                                formatter: function (val, rec) {
                                                    if (val == 1)
                                                        return '启用';
                                                    else
                                                        return '禁用';
                                                }
                                            }, {
                                                field: 'oaDeptName',
                                                title: 'oa部门',
                                                sortable: true,
                                                formatter: function (val, rec) {
                                                    if (val != null) {
                                                        return unescape(val)
                                                    }
                                                }
                                            },
                                            {
                                                field: 'superadmin',
                                                title: '是否超管',
                                                width: 50,
                                                hidden: true,
                                                formatter: function (val, rec) {
                                                    return val == '1' ? '是' : '否'
                                                }
                                            },
                                            {
                                                field: 'usersort',
                                                title: '排序',
                                                sortable: true,
                                                formatter: function (val, rec) {
                                                    return unescape(val)
                                                }
                                            },
                                            {
                                                field: 'phone',
                                                title: '电话',
                                                sortable: true,
                                                hidden: true,
                                                formatter: function (val, rec) {
                                                    if (val != null) {
                                                        return unescape(val)
                                                    }
                                                }
                                            }, {
                                                field: 'tel',
                                                title: '手机',
                                                sortable: true,
                                                hidden: true,
                                                formatter: function (val, rec) {
                                                    if (val != null) {
                                                        return unescape(val)
                                                    }
                                                }
                                            }, {
                                                field: 'casid',
                                                title: '单点账号',
                                                sortable: true,
                                                formatter: function (val, rec) {
                                                    if (val != null) {
                                                        return unescape(val)
                                                    }
                                                }
                                            }]],
                                    toolbar: [
                                        {
                                            text: '增加用户',
                                            iconCls: 'icon-add',
                                            handler: function () {
                                                //增加用户
                                                $('#ssec-form').form('clear');
                                                $('#add').window('open');
                                                var node = $('#tt').treegrid(
                                                    'getSelected');
                                                console.log('node..', node);
                                                if (node != null) {
                                                    //alert(node.DWBM);
                                                    $('#userdept').combotree(
                                                        'setValue', node.deptid);
                                                    // 设置单位编码
                                                    $('#deptCode').textbox('setValue', node.dwbm)
                                                }
                                            }
                                        },
                                        '-',
                                        {
                                            text: '编辑用户',
                                            iconCls: 'icon-edit',
                                            handler: function () {
                                                editrow();
                                            }
                                        },
                                        '-',
                                        {
                                            text: '删除用户',
                                            iconCls: 'icon-cancel',
                                            handler: function () {
                                                //删除用户
                                                var user = $('#dg').datagrid(
                                                    'getSelected');
                                                if (user) {
                                                    $.messager.confirm('提示', '确定要删除【'
                                                        + unescape(user.username)
                                                        + '】这个用户吗?', function (r) {
                                                            if (r) {
                                                                del(user);
                                                            }
                                                        });
                                                }
                                            }
                                        }, '-', {
                                            text: '查询用户',
                                            iconCls: 'icon-search',
                                            handler: function () {
                                                $('#ssec-form').form('clear');
                                                $('#search').window('open');
                                            }
                                        },],
                                    onLoadSuccess: function () {
                                    },
                                    onDblClickRow: function () {
                                        editrow();
                                    }
                                });
                        }

                        function searchName() {
                            bindgrid();
                            $('#searchform').form('clear');
                            $('#search').window('close');
                        }
                    </script>
                    <style scoped="scoped">
                        .textbox {
                            height: 20px;
                            margin: 0;
                            padding: 0 2px;
                            box-sizing: content-box;
                        }

                        /*.table tr {
height: 15px;
}*/
                    </style>

                    <div class="query_criteria" id="query_criteria">
                        <table>
                            <tr>
                                <td>
                                    <table id="tt" style="height: 500px;"></table>
                                </td>
                                <td>
                                    <table id="dg"></table>
                                </td>
                            </tr>
                        </table>
                        <div id="search" class="easyui-window" closed="true" modal="true" title="用户信息"
                            style="width: 500px; height: 300px; padding: 10px 20px">
                            <form id="searchform" class="ssec-form">
                                <table class="table">
                                    <tr>
                                        <td>
                                            <div class="ssec-label">用户名：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="searchName" class="easyui-validatebox textbox"
                                                    data-options="required:true" />
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                                <div style="padding: 5px; text-align: right;">
                                    <a class="easyui-linkbutton" onclick="searchName();" iconcls="icon-search">查询</a>
                                </div>
                            </form>
                        </div>
                        <div id="add" class="easyui-window" closed="true" modal="true" title="用户信息"
                            style="width: 500px; height: 400px; padding: 10px 10px">
                            <form id="ssec-form" class="ssec-form">
                                <table class="table">
                                    <tr style="display: none;">
                                        <td>
                                            <div class="ssec-label" style="display: none;">用户ID：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal" style="display: none;">
                                                <input id="t1" type="text" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">登陆名：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="t3" class="easyui-validatebox textbox"
                                                    data-options="required:true" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">密码：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="t4" type="password" class="easyui-textbox"
                                                    data-options="required:true" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">姓名：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="t2" class="easyui-validatebox textbox"
                                                    data-options="required:true" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">启用禁用：</div>
                                        </td>
                                        <td><select id="t5" class="easyui-combobox" data-options="required:true">
                                                <option value="1">启用</option>
                                                <option value="0">禁用</option>
                                            </select></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div>
                                                <div class="ssec-label">超管：</div>
                                            </div>
                                        </td>
                                        <td>
                                            <div>
                                                <input id="t6" type="checkbox" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">角色：</div>
                                        </td>
                                        <td><input id="roleselect" class="easyui-combogrid" style="width: 230px" /></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">所属部门：</div>
                                        </td>
                                        <td><input class="easyui-combotree" id="userdept" style="width: 230px" /></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">部门编码：</div>
                                        </td>
                                        <td><input class="easyui-textbox" id="deptCode"
                                                data-options="required:false,editable:true" style="width: 230px" /></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">oa部门编码：</div>
                                        </td>
                                        <td><input class="easyui-textbox" id="oaDeptId"
                                                data-options="required:false,editable:true" style="width: 230px" /></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">oa部门名称：</div>
                                        </td>
                                        <td><input class="easyui-textbox" id="oaDeptName"
                                                data-options="required:false,editable:true" style="width: 230px" /></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">数据权限：</div>
                                        </td>
                                        <td><input class="easyui-combotree" id="datapermission" style="width: 230px" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">直属上级：</div>
                                        </td>
                                        <td><input class="easyui-validatebox textbox" id="hlevel"
                                                style="width: 230px" /></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">排序：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="t7" class="easyui-numberbox" style="width: 50px;"
                                                    data-options="required:true" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">电话：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="phone" class="easyui-validatebox textbox" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">手机：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="txtTel" class="easyui-validatebox textbox" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">邮箱：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="txtMail" class="easyui-validatebox textbox" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">CAS：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="txtCAS" class="easyui-validatebox textbox" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">HI账号：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="txtWX" class="easyui-validatebox textbox" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">sap采购组：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="sap" class="easyui-validatebox textbox" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">oa：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="oa" class="easyui-numberbox"
                                                    data-options="min:0,precision:0" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <div class="ssec-label">按钮权限：</div>
                                        </td>
                                        <td>
                                            <div class="ssec-text normal">
                                                <input id="txtBtnPermission" style="width: 260px"
                                                    class="easyui-combobox" data-options="multiple:true,url:'sysDict/queryItemByTypeName?dicName=按钮权限',method:'get',
																			 valueField:'dicValue',textField:'dicName',panelHeight:'auto'">
                                            </div>
                                        </td>
                                    </tr>
                                </table>
                                <div style="padding: 5px; text-align: right;">
                                    <a class="easyui-linkbutton" onclick="save();" iconcls="icon-save">保存</a>
                                </div>
                            </form>
                        </div>
                    </div>
        </body>

        </html>